auto.waitFor();
//console.show();
var widthG = device.width;
var heightG = device.height
//console.log("宽：" + widthG + ",高:" + heightG);
setScreenMetrics(widthG, heightG);
var myFunc = require("./myCommonFunction.js");
var totime = 60 * 5 * 1000;
var useWiFi = true;
var closeIntenet = false;

var tohour = 111;

opreate();


function initParam() {
    var now = new Date();
    var hour = now.getHours();
    //晚上8点到早上10点用WiFi
    if (10 < hour && hour < 20) {
        useWiFi = true;
    }
    if (hour < 9 || hour == 24) {
        closeIntenet = true;
    }
}

function opreate() {
    log(new Date().toLocaleString());
    initParam();
    var ret = myFunc.initNetWork(null, useWiFi);
    threads.start(function () {
        if (ret) {
            pingduoduo();
        }
        myFunc.tuichu("拼多多");
        if (closeIntenet) {
            myFunc.closeWifiOrData(useWiFi);
        }
        exit();
    });

    threads.start(function () {
        sleep(60000)
        exit();
    });


}

function pingduoduo() {
    myFunc.openMyApp("拼多多");

    log("点击个人中心");
    var geren = text("个人中心").findOne(5000);
    if (geren == null) {
        log("没找到 个人中心");
        myFunc.tuichu("拼多多");
        sleep(500);
        var ret = myFunc.initNetWork(null, useWiFi);
        if (ret) {
            pingduoduo();
        }
        return;
    } else {
        myFunc.pClick(geren);
    }
    log("点击月卡");
    sleep(1000);
    var yueka = text("省钱月卡").findOne(5000);
    if (yueka == null) {
        log("没找到 省钱月卡");
        myFunc.tuichu("拼多多");
        sleep(500);
        pingduoduo();
        return;
    } else {
        myFunc.pClick(yueka);
    }
    sleep(2500);
    var now = new Date();
    var hour = now.getHours();
    if(hour == 8) {
        log("点击反金币");
        var shiyong = text("单单省钱").findOne(5000);
        log("单单省钱:" + (shiyong != null));
        if (shiyong == null) {
            log("没找到 单单省钱");
            myFunc.tuichu("拼多多");
            sleep(500);
            pingduoduo();
            return;
        }else {
            myFunc.pClick(shiyong);
        }
        myFunc.findOne('金币明细', 5000);
        click(150, 500);
        myFunc.findOne('领金币抵钱', 2000);
        sleep(600)
        getQuWanCheng(myFunc.findOnce('逛逛大促会场'));
        var a = myFunc.find('领取');
        for (let index = a.length-1; index >= 0; index--) {
            let element = a[index];
            myFunc.clickCenter(element)
            sleep(2000);
        }
    }
    log("点击试用");
    var shiyong = text("免费试用").findOne(5000);
    log("获取试用:" + (shiyong != null));
    if (shiyong == null) {
        log("没找到 免费试用");
        myFunc.tuichu("拼多多");
        sleep(500);
        pingduoduo();
        return;
    } else {
        myFunc.pClick(shiyong);
    }
    // className("android.view.View").text("提升概率").waitFor();
    sleep(3000);
    var tishengList = text("提升概率").findOne(5000);
    tishengList = text("提升概率").find();
    if (tishengList == null || tishengList.length == 0) {
        log("没找到 提升概率");
        myFunc.tuichu("拼多多");
        sleep(500);
        pingduoduo();
        return;
    } else {
        log("提升len:" + tishengList.length)
        for (let i = 0; i < tishengList.length; i++) {
            sleep(1000);
            tishengList[i].click();
            sleep(2000);
            var yilingqu = textEndsWith("领取").findOnce();
            if(yilingqu != null) {
                log("领过了");
            }else {
                var lingqu = text("领取奖励").findOne(3000);
                if (lingqu == null) {
                    log("没找到领取");
                } else {
                    log("领取奖励");
                    myFunc.pClick(lingqu);
                    sleep(1000);
                    text("立即领取").findOne(5000).click();
                    log("领取完成");
                    back();
                }
            } 
        }

    }

}


function getQuWanCheng(a) {
    if(a == null) {
        return null;
    }
    var b = a.bounds();
    var c = 80;
    var ret = text("去完成").boundsInside(770, b.top - c, 1080, b.bottom + c).findOnce();
    myFunc.log(a.text(), "去完成==null", ret == null);
    if (ret != null) {
        ret.click();
    }
    return ret;
}                                                                                  
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            